﻿   using System;
   using System.Web;
   using System.Web.Caching;
   using System.Web.SessionState;
   using System.Collections.Specialized;
using CY.Utility.Common;

namespace CY.Utility.Common
   {
       /// <summary>
       /// Request常用属性和操作类
       /// </summary>
       public class RequestUtility
       {
   
           #region 属性
   
           /// <summary>
           /// 当前页面是否接收到了Post请求
           /// </summary>
           public static bool IsPost
           {
               get { return SiteUtility.CurRequest.HttpMethod.Equals("POST"); }
           }
   
           /// <summary>
           /// 当前页面是否接收到了Get请求
           /// </summary>
           public static bool IsGet
           {
               get { return SiteUtility.CurRequest.HttpMethod.Equals("GET"); }
           }
   
           /// <summary>
           /// 客户端请求当前页面的URL地址
           /// </summary>
           public static string CurURL
           {
               get
               {
                   if (ConvertUtility.IsNull(SiteUtility.CurRequest.Url))
                       return string.Empty;
                   return SiteUtility.CurRequest.Url.ToString();
               }
           }
   
           /// <summary>
           /// 客户端请求当前页面的原始URL地址，不带http://的
           /// </summary>
           public static string CurRawURL
           {
               get
               {
                   if (ConvertUtility.IsNull(SiteUtility.CurRequest.RawUrl))
                       return string.Empty;
                   return SiteUtility.CurRequest.RawUrl;
               }
           }
   
           /// <summary>
           /// 上次请求的URL地址，该URL地址链接到当前URL
           /// </summary>
           public static string ReferrerURL
           {
               get
               {
                   if (ConvertUtility.IsNull(SiteUtility.CurRequest.UrlReferrer))
                       return string.Empty;
                   return SiteUtility.CurRequest.UrlReferrer.ToString();
               }
           }
   
           /// <summary>
           /// 当前请求URL的主机名
           /// </summary>
           public static string CurHostName
           {
               get
               {
                   if (ConvertUtility.IsNull(SiteUtility.CurRequest.Url))
                       return string.Empty;
                   return SiteUtility.CurRequest.Url.Host;
               }
           }
   
           /// <summary>
           /// 当前请求URL的主机名和端口信息
           /// </summary>
           public static string CurFullHostName
           {
               get
               {
                   if (!SiteUtility.CurRequest.Url.IsDefaultPort)
                   {
                       return string.Format("{0}:{1}", SiteUtility.CurRequest.Url.Host, SiteUtility.CurRequest.Url.Port.ToString());
                   }
                   return CurHostName;
               }
           }
   
           /// <summary>
           /// 当前页面文件的完整物理路径
           /// </summary>
           public static string CurPhysicalPath
           {
               get
               {
                   return SiteUtility.CurRequest.PhysicalPath;
               }
           }
   
           /// <summary>
           /// 当前页面文件名
           /// </summary>
           public static string CurPageName
           {
               get
               {
                   string[] urlArr = SiteUtility.CurRequest.Url.AbsolutePath.Split('/');
                   return urlArr[urlArr.Length - 1];
               }
           }
   
           /// <summary>
           /// 当前应用程序的完整物理路径
           /// </summary>
           public static string CurPhysicalApplicationPath
           {
               get
               {
                   return SiteUtility.CurRequest.PhysicalApplicationPath;
               }
           }
   
           /// <summary>
           /// 远程客户端的IP地址
           /// </summary>
           public static string ClientIP
           {
               get
               {
                   return SiteUtility.CurRequest.UserHostAddress;
               }
           }
   
           /// <summary>
           /// 远程客户端的DNS名称
           /// </summary>
           public static string ClientDNSName
           {
               get
               {
                   return SiteUtility.CurRequest.UserHostName;
               }
           }
   
           /// <summary>
           /// 获取当前请求Form对象
           /// </summary>
           public static NameValueCollection Form
           {
               get
               {
                   return SiteUtility.CurRequest.Form;
               }
           }
   
           /// <summary>
           /// 获取当前请求QueryString对象
           /// </summary>
           public static NameValueCollection QueryString
           {
               get
               {
                   return SiteUtility.CurRequest.QueryString;
               }
           }
   
           #endregion
   
           #region 方法
   
           /// <summary>
           /// 获取web服务器指定名称的变量值
           /// </summary>
           public static string GetServerVariable(string name)
           {
               if (SiteUtility.CurRequest.ServerVariables[name] == null)
               {
                   return "";
               }
               return SiteUtility.CurRequest.ServerVariables[name].ToString();
           }
   
           /// <summary>
           /// 获取指定名称的http头变量信息
           /// </summary>
           public static string GetHeadValue(string name)
           {
               if (SiteUtility.CurRequest.Headers[name] == null)
               {
                   return "";
               }
               return SiteUtility.CurRequest.Headers[name].ToString();
           }
   
           /// <summary>
           /// 获取string类型的url参数值
           /// </summary>
           public static string GetQueryString(string name)
           {
               return ConvertUtility.ConvertToString(SiteUtility.CurRequest.QueryString[name]);
           }
   
           /// <summary>
           /// 获取int类型的url参数值
           /// </summary>
           public static int GetQueryInt(string name, int defaultValue)
           {
               return ConvertUtility.ConvertToInt(SiteUtility.CurRequest.QueryString[name], defaultValue);
           }
   
           /// <summary>
           /// 获取float类型的url参数值
           /// </summary>
           public static float GetQueryFloat(string name, float defaultValue)
           {
               return ConvertUtility.ConvertToFloat(SiteUtility.CurRequest.QueryString[name], defaultValue);
           }
   
           /// <summary>
           /// 获取decimal类型的url参数值
           /// </summary>
           public static decimal GetQueryDecimal(string name, decimal defaultValue)
           {
               return ConvertUtility.ConvertToDecimal(SiteUtility.CurRequest.QueryString[name], defaultValue);
           }

           /// <summary>
           /// 获取long类型的url参数值
           /// </summary>
           public static long GetQueryLong(string name, long defaultValue)
           {
               return ConvertUtility.ConvertToLong(SiteUtility.CurRequest.QueryString[name], defaultValue);
           }
   
           /// <summary>
           /// 获取double类型的url参数值
           /// </summary>
           public static double GetQueryDouble(string name, double defaultValue)
           {
               return ConvertUtility.ConvertToDouble(SiteUtility.CurRequest.QueryString[name], defaultValue);
           }
   
           /// <summary>
           /// 获取DateTime类型的url参数值
           /// </summary>
           public static DateTime GetQueryDateTime(string name)
           {
               return ConvertUtility.ConvertToDateTime(SiteUtility.CurRequest.QueryString[name]);
           }
   
           /// <summary>
           /// 获取string类型的Form参数值
           /// </summary>
           public static string GetFormString(string name)
           {
               return ConvertUtility.ConvertToString(SiteUtility.CurRequest.Form[name]);
           }
   
           /// <summary>
           /// 获取int类型的Form参数值
           /// </summary>
           public static int GetFormInt(string name, int defaultValue)
           {
               return ConvertUtility.ConvertToInt(SiteUtility.CurRequest.Form[name], defaultValue);
           }

           public static long GetFormLong(string name, long defaultValue) 
           {
               return ConvertUtility.ConvertToLong(SiteUtility.CurRequest.Form[name], defaultValue);
           }
   
           /// <summary>
           /// 获取float类型的Form参数值
           /// </summary>
           public static float GetFormFloat(string name, float defaultValue)
           {
               return ConvertUtility.ConvertToFloat(SiteUtility.CurRequest.Form[name], defaultValue);
           }
   
           /// <summary>
           /// 获取double类型的Form参数值
           /// </summary>
           public static double GetFormDouble(string name, double defaultValue)
           {
               return ConvertUtility.ConvertToDouble(SiteUtility.CurRequest.Form[name], defaultValue);
           }
   
           /// <summary>
           /// 获取decimal类型的Form参数值
           /// </summary>
           public static decimal GetFormDecimal(string name, decimal defaultValue)
           {
               return ConvertUtility.ConvertToDecimal(SiteUtility.CurRequest.Form[name], defaultValue);
           }
   
           /// <summary>
           /// 获取DateTime类型的Form参数值
           /// </summary>
           public static DateTime GetFormDateTime(string name)
           {
               return ConvertUtility.ConvertToDateTime(SiteUtility.CurRequest.Form[name]);
           }
   
           /// <summary>
           /// 获取远程客户端指定名称的Cookie对象，返回HttpCookie类型
           /// </summary>
           public static HttpCookie GetCookie(string name)
           {
               return SiteUtility.CurRequestCookies[name];
           }
   
           /// <summary>
           /// 获取远程客户端指定名称的Cookie对象的值，如果为空返回string.Empty
           /// </summary>
           public static string GetCookieValue(string name)
           {
               if (ConvertUtility.IsNull(GetCookie(name)))
               {
                   return string.Empty;
               }
               return GetCookie(name).Value;
           }
   
           /// <summary>
           /// 获取远程客户端指定名称的Session对象，返回object类型
           /// </summary>
           public static object GetSession(string name)
           {
               return SiteUtility.CurSession[name];
           }
   
           /// <summary>
           /// 设置远程客户端指定名称的Cookie对象
           /// </summary>
           public static void SetResponseCookie(string name, string value, DateTime expireDateTime)
           {
               HttpCookie cookie = GetCookie(name);
               if (ConvertUtility.IsNull(cookie))
               {
                   cookie = new HttpCookie(name, value);
                   cookie.Expires = expireDateTime;
                   SiteUtility.CurResponseCookies.Add(cookie);
               }
               else
               {
                   cookie.Value = value;
                   cookie.Expires = expireDateTime;
                   SiteUtility.CurResponseCookies.Set(cookie);
               }
           }
   
           /// <summary>
           /// 设置远程客户端Cookie对象
           /// </summary>
           public static void SetResponseCookie(HttpCookie cookie)
           {
               if (GetCookie(cookie.Name) != null)
               {
                   SiteUtility.CurResponseCookies.Set(cookie);
               }
               else
               {
                   SiteUtility.CurResponseCookies.Add(cookie);
               }
           }
   
           /// <summary>
           /// 设置当前浏览器的Session对象
           /// </summary>
           public static void SetSession(string name, string value)
           {
               SiteUtility.CurSession[name] = value;
           }
   
           #endregion
       }
   }